class ApiResult {
    constructor({ code = 200, flag = false, msg = '', data = null } = {}) {
        this.code = code;
        this.flag = flag;
        this.msg = msg;
        this.data = data;
    }

    setData(data) {
        this.data = data;
        return this;
    }

    setMsg(msg) {
        this.msg = msg;
        return this;
    }

    setFlag(flag) {
        this.flag = flag;
        return this;
    }

    setCode(code) {
        this.code = code;
        return this;
    }

    toJSON() {
        return {
            code: this.code,
            flag: this.flag,
            msg: this.msg,
            data: this.data
        };
    }

   

    static success(data, msg = '操作成功') {
        return new ApiResult({
            code: 200,
            flag: true,
            msg,
            data
        });
    }

    static error(msg = '操作失败', code = 500) {
        return new ApiResult({
            code,
            flag: false,
            msg
        });
    }
}

module.exports = ApiResult;